package com.zy.serivice.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.zy.bean.TAdmin;
import com.zy.bean.TAdminExample;
import com.zy.exception.LoginException;
import com.zy.mapper.TAdminMapper;
import com.zy.service.TAdminService;
import com.zy.util.AppDateUtils;
import com.zy.util.Const;
import com.zy.util.MD5Util;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;

import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;


@Service
public class TAdminServiceImpl implements TAdminService {

    @Autowired
    TAdminMapper adminMapper;

//	@Override
//	public TAdmin getTAdminByLogin(Map<String, Object> paramMap) {
//
//		//1.密码加密
//
//
//		//2.查询用户
//		String loginacct = (String)paramMap.get("loginacct");
//		String userpswd = (String)paramMap.get("userpaswd");
//
//
//		//3.判断用户是否为null
//		TAdminExample example = new TAdminExample();
//
//		example.createCriteria().andLoginacctEqualTo(loginacct);
//
//		List<TAdmin> list = adminMapper.selectByExample(example);
//
//		if(list!=null && list.size()==1) {
//			TAdmin admin = list.get(0);
//
//			//4.判断密码是否一致
//			if(admin.getUserpswd().equals(userpswd)) {
//				//5.返回结果
//				return admin ;
//			}else {
//				throw new LoginException(Const.LOGIN_USERPSWD_ERROR);
//			}
//
//		}else {
//			throw new LoginException(Const.LOGIN_LOGINACCT_ERROR);
//		}
//	}


    @Override
    public TAdmin getTAdminByLogin(Map<String, Object> paramMap) {

        //1.密码加密


        //2.查询用户
        String loginacct = (String) paramMap.get("loginacct");
        String userpswd = (String) paramMap.get("userpaswd");


        //3.判断用户是否为null
        TAdminExample example = new TAdminExample();

        example.createCriteria().andLoginacctEqualTo(loginacct);

        List<TAdmin> list = adminMapper.selectByExample(example);

        if (list == null || list.size() == 0) {
            throw new LoginException(Const.LOGIN_LOGINACCT_ERROR);
        }

        TAdmin admin = list.get(0);

        //4.判断密码是否一致
        if (!admin.getUserpswd().equals(MD5Util.digest(userpswd))) {
            throw new LoginException(Const.LOGIN_USERPSWD_ERROR);
        }

        //5.返回结果
        return admin;

    }

    @Override
    public PageInfo<TAdmin> listAdminPage(Map<String, Object> paramMap) {

        String condition = (String) paramMap.get("condition");

        TAdminExample examle = new TAdminExample();

        if (!"".equals(condition)) {
            examle.createCriteria().andLoginacctLike("%" + condition + "%");

            TAdminExample.Criteria criteria2 = examle.createCriteria();
            criteria2.andUsernameLike("%" + condition + "%");

            TAdminExample.Criteria criteria3 = examle.createCriteria();
            criteria3.andEmailLike("%" + condition + "%");

            examle.or(criteria2);
            examle.or(criteria3);
        }


        //examle.setOrderByClause("createtime desc");//添加完了回到第一页

        List<TAdmin> list = adminMapper.selectByExample(examle);

        PageInfo<TAdmin> page = new PageInfo<TAdmin>(list, 5);

        return page;
    }

    @Override
    public Integer saveAdmin(TAdmin admin) {
        TAdminExample examle = new TAdminExample();
        examle.createCriteria().andLoginacctEqualTo(admin.getLoginacct());
        List<TAdmin> tAdmins = adminMapper.selectByExample(examle);
        int i = 0;
        if (tAdmins.size() == 0) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            String date = df.format(new Date());
            admin.setCreatetime(date);
            admin.setUserpswd("e10adc3949ba59abbe56e057f20f883e");
            String username = admin.getUsername();

            i = adminMapper.insertSelective(admin);

            return i;
        }
        return i;

    }

    @Override
    public TAdmin selectbyId(Integer id) {
        TAdmin admin = adminMapper.selectByPrimaryKey(id);
        return admin;
    }

    @Override
    public void updateAdmin(TAdmin admin) {
        adminMapper.updateByPrimaryKeySelective(admin);
    }

    @Override
    public void delbyId(Integer id) {
        int i = adminMapper.deleteByPrimaryKey(id);


    }


    /**
     * @Author zy 690748807@qq,com
     * @Date ${DATE} ${TIME}
     * @Version 1.5
     * @name 方法名  批量删除
     */
    @Override
    public void delbyIds( List<Integer> list) {
        adminMapper.deleteByAll(list);
    }

    public static void main(String[] args) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String date = df.format(new Date());
        System.out.println("当前时间：" + date);


    }


}
